/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.foxline.manager;

import br.com.foxline.enums.TypeDocument;
import br.com.foxline.model.ActCep;
import br.com.foxline.model.Charter;
import br.com.foxline.model.City;
import br.com.foxline.model.Country;
import br.com.foxline.model.NatureWriting;
import br.com.foxline.model.Part;
import br.com.foxline.model.People;
import br.com.foxline.model.Quality;
import br.com.foxline.service.ActCepService;
import br.com.foxline.service.CharterService;
import br.com.foxline.service.CityService;
import br.com.foxline.service.CountryService;
import br.com.foxline.service.NatureWritingService;
import br.com.foxline.service.PeopleService;
import br.com.foxline.service.QualityService;
import br.com.foxline.util.Caracter;
import br.com.foxline.util.Msg;
import java.io.BufferedReader;
import java.io.FileReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

/**
 *
 * @author ely
 */
@ManagedBean
@ViewScoped
public class ManagerReadFile {

    @EJB
    CityService cityService;
    @EJB
    CharterService charterService;
    @EJB
    CountryService countryService;
    @EJB
    ActCepService actCepService;
    @EJB
    NatureWritingService natureWritingService;
    @EJB
    PeopleService peopleService;
    @EJB
    QualityService qualityService;
    private Charter charter;
    private Country country;
    private City city;
    private ActCep act, actCep;
    private NatureWriting natureWriting;
    private Part partOutorgado, partOutorgante;
    private People outorgado, oucpf, outcpf, oucnpj, outcnpj;
    private People outorgante;
    private List<ActCep> generatorsActs;
    private List<NatureWriting> generatorNatureWritings;
    private List<Part> parts;
    private Quality qualityOutorgado, qualityOutorgante;

    public void ManagerReadFile() {
    } 

    @PostConstruct
    public void init() {
        charter = new Charter();
        partOutorgado = new Part();
        partOutorgante = new Part();
        outorgado = new People();
        outorgante = new People();
        generatorsActs = new ArrayList<ActCep>();
        generatorNatureWritings = new ArrayList<NatureWriting>();
        parts = new ArrayList<Part>();


    }
 
    public void read() {
        String l = null;

        try {
 
            FileReader reader = new FileReader("/Users/ely/Documents/file 2.txt");
            BufferedReader read = new BufferedReader(reader);
 
            StringTokenizer st = null; 

            String liv;
            String fls;
            String esc;
            String out;
            String oud;
            String obj;
            String data;
            String tip;
            String cpfof;
            String cpfoj;
            String cpfdf;
            String cpfdj;
            String nullo;

            while ((l = read.readLine()) != null) {

                st = new StringTokenizer(l, ";");
                String dados = null;


                while (st.hasMoreTokens()) {

                    dados = st.nextToken();
                    liv = dados;

                    dados = st.nextToken();
                    fls = dados;

                    dados = st.nextToken();
                    esc = dados;
 
                    dados = st.nextToken();
                    out = dados;

                    dados = st.nextToken();
                    oud = dados;

                    dados = st.nextToken();
                    obj = dados;

                    dados = st.nextToken();
                    data = dados;

                    dados = st.nextToken();
                    tip = dados;

                    dados = st.nextToken();
                    cpfof = dados;

                    dados = st.nextToken();
                    cpfoj = dados;

                    dados = st.nextToken();
                    cpfdf = dados;

                    dados = st.nextToken();
                    cpfdj = dados;


                    // Jogando Valores  

                    String num = liv.replaceAll("\\D*", "");
                    String let = liv.replaceAll("\\d*", "");

                    String numF = fls.replaceAll("\\D*", "");
                    String letF = fls.replaceAll("\\d*", "");


                    charter.setBook(num);
                    charter.setBookComp(let);

                    charter.setLeaf(numF);
                    charter.setLeafComp(letF);

                    charter.setDateAct(converterData(data));

                    charter.setEsc(esc);

                    charter.setDescription(obj);

                    city = cityService.find(3493L);
                    charter.setCity(city);
 
                    country = countryService.find(18L);
                    charter.setUf(country);

                    actCep = actCepService.findActCod("1");

 
                    if (out.equals(".") || out.equals("")) {
                        System.out.println("Achou Outorgado" + out);
                        outorgante.setName(oud);
                        outorgante.setCpf(Caracter.removecaracter(cpfdf));
                        outorgante.setCnpj(Caracter.removecaracter(cpfdj));
                        qualityOutorgante = qualityService.findQualityCod("2");

                        if (outorgante.getCpf().equals("")) {
                            outorgante.setCpf(null);
                        }
                        if (outorgante.getCnpj().equals("")) {
                            outorgante.setCnpj(null);
                        }

                        outcpf = peopleService.existPeople(TypeDocument.CPF, outorgante.getCpf());

                        if (outcpf != null) {
                            outorgante = peopleService.find(outcpf.getId());

                        }
                        outcnpj = peopleService.existPeople(TypeDocument.CNPJ, outorgante.getCnpj());
                        if (outcnpj != null) {
                            outorgante = peopleService.find(outcnpj.getId());
                        }




                        // Verifica o Tipo do Sistema Acess e Joga para os Valores de acordo com o Censec
                        if (tip.equals("4")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("5")) {
                            natureWriting = natureWritingService.findNatureCod("22");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("6")) {
                            natureWriting = natureWritingService.findNatureCod("46");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("7")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("8")) {
                            natureWriting = natureWritingService.findNatureCod("21");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("9")) {
                            natureWriting = natureWritingService.findNatureCod("14");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("10")) {
                            natureWriting = natureWritingService.findNatureCod("25");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("12")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("13")) {
                            natureWriting = natureWritingService.findNatureCod("62");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("14")) {
                            natureWriting = natureWritingService.findNatureCod("35");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("15")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("16")) {
                            actCep = actCepService.findActCod("2");
                            natureWriting = natureWritingService.findNatureCod("61");
                            charter.setNatureWriting(natureWriting);
                        } 
                        if (tip.equals("18")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // FALTA A NATUREZA DO ATO COMPROMISSO DO ACESS
                        }
                        if (tip.equals("19")) {
                            natureWriting = natureWritingService.findNatureCod("14");
                            charter.setNatureWriting(natureWriting);

                            // DÚVIDAS ADITAMENTO = DECLARAÇÃO
                        }
                        if (tip.equals("20")) {
                            natureWriting = natureWritingService.findNatureCod("15");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("22")) {
                            natureWriting = natureWritingService.findNatureCod("36");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("23")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("24")) {
                            natureWriting = natureWritingService.findNatureCod("23");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("25")) {
                            natureWriting = natureWritingService.findNatureCod("4");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("26")) {
                            natureWriting = natureWritingService.findNatureCod("50");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("27")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("28")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("29")) {
                            natureWriting = natureWritingService.findNatureCod("62");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("30")) {
                            natureWriting = natureWritingService.findNatureCod("61");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("31")) {
                            natureWriting = natureWritingService.findNatureCod("53");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("32")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // SUB-CESSÃO DE DIREITO
                        }
                        if (tip.equals("33")) {
                            natureWriting = natureWritingService.findNatureCod("30");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("34")) {
                            natureWriting = natureWritingService.findNatureCod("31");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("35")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("36")) {
                            natureWriting = natureWritingService.findNatureCod("25");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("37")) {
                            natureWriting = natureWritingService.findNatureCod("5");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("38")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // TRANSFERENCIA
                        }
                        if (tip.equals("39")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // CONTRATO
                        }
                        if (tip.equals("40")) {
                            natureWriting = natureWritingService.findNatureCod("57");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("41")) {
                            natureWriting = natureWritingService.findNatureCod("57");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("42")) {
                            natureWriting = natureWritingService.findNatureCod("35");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("43")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("44")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // CAUÇÃO
                        }
                        if (tip.equals("45")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("46")) {
                            natureWriting = natureWritingService.findNatureCod("22");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("47")) {
                            natureWriting = natureWritingService.findNatureCod("49");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("48")) {
                            natureWriting = natureWritingService.findNatureCod("24");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("49")) {
                            natureWriting = natureWritingService.findNatureCod("53");
                            charter.setNatureWriting(natureWriting);
                        }

                        if (tip.equals("50")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            //ANUNCIA
                        }
                        if (tip.equals("51")) {
                            natureWriting = natureWritingService.findNatureCod("17");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("52")) {
                            natureWriting = natureWritingService.findNatureCod("28");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("53")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // EMPREITADA
                        }
                        if (tip.equals("54")) {
                            natureWriting = natureWritingService.findNatureCod("54");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("55")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // ARRENDAMENTO

                        }
                        if (tip.equals("56")) {
                            natureWriting = natureWritingService.findNatureCod("34");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("57")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // AQUISIÇÃO
                        }
                        if (tip.equals("58")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // PROCURAÇÃO
                        }
                        if (tip.equals("59")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // SUBSTITUIÇÃO
                        }
                        if (tip.equals("60")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // COMPOSIÇÃO
                        }


                        if (tip.equals("1") || tip.equals("2") || tip.equals("3") || tip.equals("17") || tip.equals("21") || tip.equals("19") || tip.equals("43")) {
                            System.out.println("Arquivos do Cesdi - " + charter.getBook());


                        } else {
                            charter.setActCep(actCep);


                            partOutorgante.setPeople(outorgante);
                            partOutorgante.setPartQuality(qualityOutorgante);

                            parts.add(partOutorgante);
                            charter.setParts(parts);


                            charterService.update(charter);
                            outorgado = new People();
                            charter = new Charter();
                            partOutorgado = new Part();
                            outorgante = new People();
                            partOutorgante = new Part();
                            parts = new ArrayList<Part>();
                        }

                    } else if (oud.equals(".") || oud.equals("")) {
                        System.out.println("Achou Outorgante" + oud);
                        outorgado.setName(out);
                        outorgado.setCpf(Caracter.removecaracter(cpfof));
                        outorgado.setCnpj(Caracter.removecaracter(cpfoj));
                        qualityOutorgado = qualityService.findQualityCod("1");
                        if (outorgado.getCpf().equals("")) {
                            outorgado.setCpf(null);
                        }
                        if (outorgado.getCnpj().equals("")) {
                            outorgado.setCnpj(null);
                        }

                        oucpf = peopleService.existPeople(TypeDocument.CPF, outorgado.getCpf());
                        if (oucpf != null) {
                            outorgado = peopleService.find(oucpf.getId());
                        }
                        oucnpj = peopleService.existPeople(TypeDocument.CNPJ, outorgado.getCnpj());
                        if (oucnpj != null) {
                            outorgado = peopleService.find(oucnpj.getId());
                        }



                        // Verifica o Tipo do Sistema Acess e Joga para os Valores de acordo com o Censec
                        if (tip.equals("4")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("5")) {
                            natureWriting = natureWritingService.findNatureCod("22");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("6")) {
                            natureWriting = natureWritingService.findNatureCod("46");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("7")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("8")) {
                            natureWriting = natureWritingService.findNatureCod("21");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("9")) {
                            natureWriting = natureWritingService.findNatureCod("14");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("10")) {
                            natureWriting = natureWritingService.findNatureCod("25");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("12")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("13")) {
                            natureWriting = natureWritingService.findNatureCod("62");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("14")) {
                            natureWriting = natureWritingService.findNatureCod("35");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("15")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("16")) {
                            actCep = actCepService.findActCod("2");
                            natureWriting = natureWritingService.findNatureCod("61");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("18")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // FALTA A NATUREZA DO ATO COMPROMISSO DO ACESS
                        }
                        if (tip.equals("19")) {
                            natureWriting = natureWritingService.findNatureCod("14");
                            charter.setNatureWriting(natureWriting);

                            // DÚVIDAS ADITAMENTO = DECLARAÇÃO
                        }
                        if (tip.equals("20")) {
                            natureWriting = natureWritingService.findNatureCod("15");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("22")) {
                            natureWriting = natureWritingService.findNatureCod("36");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("23")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("24")) {
                            natureWriting = natureWritingService.findNatureCod("23");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("25")) {
                            natureWriting = natureWritingService.findNatureCod("4");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("26")) {
                            natureWriting = natureWritingService.findNatureCod("50");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("27")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("28")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("29")) {
                            natureWriting = natureWritingService.findNatureCod("62");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("30")) {
                            natureWriting = natureWritingService.findNatureCod("61");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("31")) {
                            natureWriting = natureWritingService.findNatureCod("53");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("32")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // SUB-CESSÃO DE DIREITO
                        }
                        if (tip.equals("33")) {
                            natureWriting = natureWritingService.findNatureCod("30");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("34")) {
                            natureWriting = natureWritingService.findNatureCod("31");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("35")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("36")) {
                            natureWriting = natureWritingService.findNatureCod("25");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("37")) {
                            natureWriting = natureWritingService.findNatureCod("5");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("38")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // TRANSFERENCIA
                        }
                        if (tip.equals("39")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // CONTRATO
                        }
                        if (tip.equals("40")) {
                            natureWriting = natureWritingService.findNatureCod("57");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("41")) {
                            natureWriting = natureWritingService.findNatureCod("57");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("42")) {
                            natureWriting = natureWritingService.findNatureCod("35");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("43")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("44")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // CAUÇÃO
                        }
                        if (tip.equals("45")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("46")) {
                            natureWriting = natureWritingService.findNatureCod("22");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("47")) {
                            natureWriting = natureWritingService.findNatureCod("49");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("48")) {
                            natureWriting = natureWritingService.findNatureCod("24");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("49")) {
                            natureWriting = natureWritingService.findNatureCod("53");
                            charter.setNatureWriting(natureWriting);
                        }

                        if (tip.equals("50")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            //ANUNCIA
                        }
                        if (tip.equals("51")) {
                            natureWriting = natureWritingService.findNatureCod("17");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("52")) {
                            natureWriting = natureWritingService.findNatureCod("28");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("53")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // EMPREITADA
                        }
                        if (tip.equals("54")) {
                            natureWriting = natureWritingService.findNatureCod("54");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("55")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // ARRENDAMENTO

                        }
                        if (tip.equals("56")) {
                            natureWriting = natureWritingService.findNatureCod("34");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("57")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // AQUISIÇÃO
                        }
                        if (tip.equals("58")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // PROCURAÇÃO
                        }
                        if (tip.equals("59")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // SUBSTITUIÇÃO
                        }
                        if (tip.equals("60")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // COMPOSIÇÃO
                        }


                        if (tip.equals("1") || tip.equals("2") || tip.equals("3") || tip.equals("17") || tip.equals("21") || tip.equals("19") || tip.equals("43")) {
                            System.out.println("Arquivos do Cesdi - " + charter.getBook());


                        } else {
                            charter.setActCep(actCep);
                            partOutorgado.setPeople(outorgado);
                            partOutorgado.setPartQuality(qualityOutorgado);


                            parts.add(partOutorgado);

                            charter.setParts(parts);


                            charterService.update(charter);
                            outorgado = new People();
                            charter = new Charter();
                            partOutorgado = new Part();
                            outorgante = new People();
                            partOutorgante = new Part();
                            parts = new ArrayList<Part>();
                        }

                    } else {

                        outorgado.setName(out);
                        outorgado.setCpf(Caracter.removecaracter(cpfof));
                        outorgado.setCnpj(Caracter.removecaracter(cpfoj));
                        qualityOutorgado = qualityService.findQualityCod("1");
                        if (outorgado.getCpf().equals("")) {
                            outorgado.setCpf(null);
                        }
                        if (outorgado.getCnpj().equals("")) {
                            outorgado.setCnpj(null);
                        }

                        oucpf = peopleService.existPeople(TypeDocument.CPF, outorgado.getCpf());
                        if (oucpf != null) {
                            outorgado = peopleService.find(oucpf.getId());
                        }
                        oucnpj = peopleService.existPeople(TypeDocument.CNPJ, outorgado.getCnpj());
                        if (oucnpj != null) {
                            outorgado = peopleService.find(oucnpj.getId());
                        }



                        outorgante.setName(oud);
                        outorgante.setCpf(Caracter.removecaracter(cpfdf));
                        outorgante.setCnpj(Caracter.removecaracter(cpfdj));
                        qualityOutorgante = qualityService.findQualityCod("2");

                        if (outorgante.getCpf().equals("")) {
                            outorgante.setCpf(null);
                        }
                        if (outorgante.getCnpj().equals("")) {
                            outorgante.setCnpj(null);
                        }

                        outcpf = peopleService.existPeople(TypeDocument.CPF, outorgante.getCpf());

                        if (outcpf != null) {
                            outorgante = peopleService.find(outcpf.getId());

                        }
                        outcnpj = peopleService.existPeople(TypeDocument.CNPJ, outorgante.getCnpj());
                        if (outcnpj != null) {
                            outorgante = peopleService.find(outcnpj.getId());
                        }




                        // Verifica o Tipo do Sistema Acess e Joga para os Valores de acordo com o Censec
                        if (tip.equals("4")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("5")) {
                            natureWriting = natureWritingService.findNatureCod("22");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("6")) {
                            natureWriting = natureWritingService.findNatureCod("46");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("7")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("8")) {
                            natureWriting = natureWritingService.findNatureCod("21");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("9")) {
                            natureWriting = natureWritingService.findNatureCod("14");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("10")) {
                            natureWriting = natureWritingService.findNatureCod("25");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("12")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("13")) {
                            natureWriting = natureWritingService.findNatureCod("62");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("14")) {
                            natureWriting = natureWritingService.findNatureCod("35");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("15")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("16")) {
                            actCep = actCepService.findActCod("2");
                            natureWriting = natureWritingService.findNatureCod("61");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("18")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // FALTA A NATUREZA DO ATO COMPROMISSO DO ACESS
                        }
                        if (tip.equals("19")) {
                            natureWriting = natureWritingService.findNatureCod("14");
                            charter.setNatureWriting(natureWriting);

                            // DÚVIDAS ADITAMENTO = DECLARAÇÃO
                        }
                        if (tip.equals("20")) {
                            natureWriting = natureWritingService.findNatureCod("15");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("22")) {
                            natureWriting = natureWritingService.findNatureCod("36");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("23")) {
                            natureWriting = natureWritingService.findNatureCod("6");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("24")) {
                            natureWriting = natureWritingService.findNatureCod("23");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("25")) {
                            natureWriting = natureWritingService.findNatureCod("4");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("26")) {
                            natureWriting = natureWritingService.findNatureCod("50");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("27")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("28")) {
                            natureWriting = natureWritingService.findNatureCod("52");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("29")) {
                            natureWriting = natureWritingService.findNatureCod("62");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("30")) {
                            natureWriting = natureWritingService.findNatureCod("61");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("31")) {
                            natureWriting = natureWritingService.findNatureCod("53");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("32")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // SUB-CESSÃO DE DIREITO
                        }
                        if (tip.equals("33")) {
                            natureWriting = natureWritingService.findNatureCod("30");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("34")) {
                            natureWriting = natureWritingService.findNatureCod("31");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("35")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("36")) {
                            natureWriting = natureWritingService.findNatureCod("25");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("37")) {
                            natureWriting = natureWritingService.findNatureCod("5");
                            charter.setNatureWriting(natureWriting);

                        }
                        if (tip.equals("38")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // TRANSFERENCIA
                        }
                        if (tip.equals("39")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // CONTRATO
                        }
                        if (tip.equals("40")) {
                            natureWriting = natureWritingService.findNatureCod("57");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("41")) {
                            natureWriting = natureWritingService.findNatureCod("57");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("42")) {
                            natureWriting = natureWritingService.findNatureCod("35");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("43")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("44")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // CAUÇÃO
                        }
                        if (tip.equals("45")) {
                            natureWriting = natureWritingService.findNatureCod("10");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("46")) {
                            natureWriting = natureWritingService.findNatureCod("22");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("47")) {
                            natureWriting = natureWritingService.findNatureCod("49");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("48")) {
                            natureWriting = natureWritingService.findNatureCod("24");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("49")) {
                            natureWriting = natureWritingService.findNatureCod("53");
                            charter.setNatureWriting(natureWriting);
                        }

                        if (tip.equals("50")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            //ANUNCIA
                        }
                        if (tip.equals("51")) {
                            natureWriting = natureWritingService.findNatureCod("17");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("52")) {
                            natureWriting = natureWritingService.findNatureCod("28");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("53")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // EMPREITADA
                        }
                        if (tip.equals("54")) {
                            natureWriting = natureWritingService.findNatureCod("54");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("55")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // ARRENDAMENTO

                        }
                        if (tip.equals("56")) {
                            natureWriting = natureWritingService.findNatureCod("34");
                            charter.setNatureWriting(natureWriting);
                        }
                        if (tip.equals("57")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // AQUISIÇÃO
                        }
                        if (tip.equals("58")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // PROCURAÇÃO
                        }
                        if (tip.equals("59")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // SUBSTITUIÇÃO
                        }
                        if (tip.equals("60")) {
                            natureWriting = null;
                            charter.setNatureWriting(natureWriting);

                            // COMPOSIÇÃO
                        }



                        if (tip.equals("1") || tip.equals("2") || tip.equals("3") || tip.equals("17") || tip.equals("21") || tip.equals("19") || tip.equals("43")) {
                            System.out.println("Arquivos do Cesdi - " + charter.getBook());


                        } else {
                            charter.setActCep(actCep);
                            partOutorgado.setPeople(outorgado);
                            partOutorgado.setPartQuality(qualityOutorgado);
                            partOutorgante.setPeople(outorgante);
                            partOutorgante.setPartQuality(qualityOutorgante);
                            parts.add(partOutorgado);
                            parts.add(partOutorgante);
                            charter.setParts(parts);


                            charterService.update(charter);
                            outorgado = new People();
                            charter = new Charter();
                            partOutorgado = new Part();
                            outorgante = new People();
                            partOutorgante = new Part();
                            parts = new ArrayList<Part>();
                        }


                    }
                }

            }

            read.close();
            reader.close();

        } catch (Exception erro) {
            erro.printStackTrace();
            System.err.println(erro);
        }
    }

    public void generatorActCep() {

        generatorsActs.add(new ActCep("1", "Escritura"));
        generatorsActs.add(new ActCep("2", "Procuração"));
        generatorsActs.add(new ActCep("3", "Procuração para fins Previdenciários"));
        generatorsActs.add(new ActCep("5", "Renúncia de Procuração"));
        generatorsActs.add(new ActCep("6", "Revogação de Procuração"));
        generatorsActs.add(new ActCep("7", "Substabelecimento"));
        generatorsActs.add(new ActCep("8", "Ata Notarial"));

        for (ActCep actCep : generatorsActs) {
            actCepService.save(actCep);
            Msg.messagemInfo("Gerando - " + actCep.getDescription());
        }

    }

    public void generatorNatureWriting() {

        act = actCepService.find(1L);

        System.out.println(act);

        generatorNatureWritings.add(new NatureWriting("1", "Acordo ExtraJudicial", act));
        generatorNatureWritings.add(new NatureWriting("4", "Alienação Fiduciária", act));
        generatorNatureWritings.add(new NatureWriting("26", "Bem de Família", act));
        generatorNatureWritings.add(new NatureWriting("5", "Cessão ", act));
        generatorNatureWritings.add(new NatureWriting("63", "Comodato", act));
        generatorNatureWritings.add(new NatureWriting("6", "Compra e Venda", act));
        generatorNatureWritings.add(new NatureWriting("48", "Condomínio Edilício", act));
        generatorNatureWritings.add(new NatureWriting("56", "Conferência de Bens", act));
        generatorNatureWritings.add(new NatureWriting("10", "Confissão de Dívidas/Dação e Pagamento", act));
        generatorNatureWritings.add(new NatureWriting("58", "Crédito com Garantia", act));
        generatorNatureWritings.add(new NatureWriting("14", "Declaração", act));
        generatorNatureWritings.add(new NatureWriting("15", "Declaratória de União Estável", act));
        generatorNatureWritings.add(new NatureWriting("16", "Declaratório de União Estável Homoafetiva", act));
        generatorNatureWritings.add(new NatureWriting("17", "Desapropriação ", act));
        generatorNatureWritings.add(new NatureWriting("52", "Direito de Uso ou Superfície", act));
        generatorNatureWritings.add(new NatureWriting("55", "Diretivas Antecipadas de Vontade(Testamento Vital)", act));
        generatorNatureWritings.add(new NatureWriting("17", "Desapropriação ", act));
        generatorNatureWritings.add(new NatureWriting("20", "Dissolução de União Estável ", act));
        generatorNatureWritings.add(new NatureWriting("21", "Distrato ", act));
        generatorNatureWritings.add(new NatureWriting("53", "Divisão ", act));
        generatorNatureWritings.add(new NatureWriting("22", "Doação ", act));
        generatorNatureWritings.add(new NatureWriting("23", "Emancipação ", act));
        generatorNatureWritings.add(new NatureWriting("59", "Emissão de Cédula", act));
        generatorNatureWritings.add(new NatureWriting("60", "Emissão de Debêntures", act));
        generatorNatureWritings.add(new NatureWriting("54", "Fiança ", act));
        generatorNatureWritings.add(new NatureWriting("24", "Hipoteca ", act));
        generatorNatureWritings.add(new NatureWriting("25", "Incorporação ", act));
        generatorNatureWritings.add(new NatureWriting("57", "Novação ", act));
        generatorNatureWritings.add(new NatureWriting("30", "Pacto Antenupcial ", act));
        generatorNatureWritings.add(new NatureWriting("49", "Parcelamento ", act));
        generatorNatureWritings.add(new NatureWriting("31", "Penhor", act));
        generatorNatureWritings.add(new NatureWriting("33", "Promessa de Cessão  de Direitos Aquisitivos", act));
        generatorNatureWritings.add(new NatureWriting("34", "Quitação", act));
        generatorNatureWritings.add(new NatureWriting("36", "Reconhecimento de Paternidade", act));
        generatorNatureWritings.add(new NatureWriting("38", "Registro de Chancela Mecânica", act));
        generatorNatureWritings.add(new NatureWriting("39", "Remissão de Foros e Laudêmios", act));
        generatorNatureWritings.add(new NatureWriting("62", "Renuncia de Direitos Hereditários", act));
        generatorNatureWritings.add(new NatureWriting("35", "Rerratificação", act));
        generatorNatureWritings.add(new NatureWriting("61", "Revogação", act));
        generatorNatureWritings.add(new NatureWriting("43", "Sem valor Declarado", act));
        generatorNatureWritings.add(new NatureWriting("45", "Servidão", act));
        generatorNatureWritings.add(new NatureWriting("50", "Sociedade e Fundação", act));
        generatorNatureWritings.add(new NatureWriting("51", "Transação", act));
        generatorNatureWritings.add(new NatureWriting("46", "Usufruto(reserva, instituição e renúncia)", act));

        for (NatureWriting natureWriting : generatorNatureWritings) {

            natureWritingService.save(natureWriting);
            Msg.messagemInfo("Gerando - " + natureWriting.getDescription());

        }

    }

    public Date converterData(String data) {
        if (data == null || data.equals("") || data.equals(".")) {
            return null;
        }

        Date date = null;
        try {
            DateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
            date = (Date) formater.parse(data);





        } catch (ParseException ex) {
            Logger.getLogger(ManagerReadFile.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
        return date;
    }
}
// Inventário - Cesdi
// Divórcio - Cesdi
// Separação - Cesdi
// Testamento - Cesdi        
// SobrePartilha - Cesdi
// Aditamento - Rcto
// Comunhão de bens - Cesdi